

package jetbrains.buildServer.server.rest.errors;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
import jetbrains.buildServer.server.rest.jersey.ExceptionMapperBase;
import jetbrains.buildServer.serverSide.*;
import jetbrains.buildServer.serverSide.identifiers.DuplicateExternalIdException;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;

/**
 * User: Yegor Yarko
 * Date: 30.03.2009
 */
@Provider
@Component
public class RuntimeExceptionMapper extends ExceptionMapperBase<RuntimeException> {
  @Override
  public ResponseData getResponseData(@NotNull final RuntimeException e) {
    // process known errors
    if (e instanceof DuplicateProjectNameException ||
        e instanceof InvalidIdentifierException ||
        e instanceof DuplicateBuildTypeIdException ||
        e instanceof DuplicateBuildTypeNameException ||
        e instanceof DuplicateExternalIdException ||
        e instanceof DuplicateIdException ||
        e instanceof DuplicateTemplateNameException ||
        e instanceof InvalidNameException) {
      return new ResponseData(Response.Status.BAD_REQUEST, "Error occurred while processing this request.");
    }

    return new ResponseData(Response.Status.INTERNAL_SERVER_ERROR, "Error occurred while processing this request.");
  }
}